package cn.kent.base.interrupt;

/**
 * 通过interrupt方法将子线程从sleep阻塞状态返回激活状态
 */
public class InterruptTest2 {
    public static void main(String[] args) throws InterruptedException {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("threadA begin sleep 2000s...");
                    // 休眠2000s
                    Thread.sleep(2000000);
                    System.out.println("threadA end sleep 2000s...");
                } catch (InterruptedException e) {
                    System.out.println("threadA is interrupted while sleeping");
                    return;
                }
            }
        });

        threadA.start();
        // 确保子线程进入休眠
        Thread.sleep(2000);
        // 主线程将子线程sleep终止,让子线程从sleep函数返回
        threadA.interrupt();
        threadA.join();
        System.out.println("main thread is over");
    }
}
